package ctn.dao.hibernate.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Component;

import ctn.contant.MsgContant;
import ctn.dao.inteface.MessageDao;
import ctn.hibernate.po.CtnMsgTempl;
import ctn.hibernate.po.CtnUserMsg;

@Component
public class MessageDaoImpl extends AbstractCTNDAO implements MessageDao {

	@Override
	@SuppressWarnings("unchecked")
	public List<Map<String, String>> getMessageQueuec(){
		List<Map<String, String>> m = (List<Map<String, String>>) getHibernateTemplate().executeFind(new HibernateCallback() {
			@Override
			public List<Map<String, String>> doInHibernate(Session session)
					throws HibernateException, SQLException {
				StringBuffer sb = new StringBuffer(" select m.msg_templ_id,t.content,t.subject,s.email,m.id,m.user_id,m.send_user_id ");
				sb.append(" from ctn_user_msg m,ctn_msg_templ t,ctn_user s ");
				sb.append(" where m.status <> 'S' and m.status <>'R' and m.msg_templ_id = t.id and s.id = m.send_user_id ");
				sb.append(" order by t.id ");
				SQLQuery qry = session.createSQLQuery(sb.toString());
				List<Object[]> list = qry.list();
				Integer id = 0;
				String subject = "";
				String content = "";
				String tomail = "";
				Integer uid = 0;
				Integer sid = 0;
				Integer suid = 0;
				List<Map<String, String>> listMap = new ArrayList<Map<String,String>>();
				for (Object[] o : list) {
					id = (Integer)o[0];
					content = (String)o[1];
					subject = (String)o[2];
					tomail = (String)o[3];
					uid = (Integer)o[4];
					sid = (Integer)o[5];
					suid = (Integer)o[6];
					sb.setLength(0);
					sb.append(" select u.parm,u.parm_val ");
					sb.append(" from ctn_user_msg_parm u ");
					sb.append(" where u.msg_id = ").append(uid);
					SQLQuery query = session.createSQLQuery(sb.toString());
					List<Object[]> listRepact = query.list();
					for (Object[] o2 : listRepact) {
						content = content.replaceAll("<"+(String)o2[0]+">", (String)o2[1]);
						subject = subject.replaceAll("<"+(String)o2[0]+">", (String)o2[1]);
					}
					Map<String, String> map = new HashMap<String, String>();
					map.put(MsgContant.MESSAGE_SUBJECT, subject);
					map.put(MsgContant.MESSAGE_CONTENT, content);
					map.put(MsgContant.MESSAGE_TO_MAIL, tomail);
					map.put(MsgContant.MESSAGE_ID, uid+"");
					map.put(MsgContant.MESSAGE_RECEVIE_USER_ID, sid+"");
					map.put(MsgContant.MESSAGE_SEND_USER_ID, suid+"");
					map.put(MsgContant.MESSAGE_TEMPLATE_ID, id+"");
					listMap.add(map);
				}
				return listMap;
			}
			
		});
		
		return m;
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean updateMsgStatus(final String status,final int id) {
		Integer result = (Integer)getHibernateTemplate().execute(new HibernateCallback() {
			@Override
			public Integer doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				SQLQuery qry = arg0.createSQLQuery("update ctn_user_msg s set s.status=:status where s.ID=:id");
				qry.setParameter("status", status);
				qry.setParameter("id", id);
				int result = qry.executeUpdate();
				return result;
			}
			
		});
		return result.intValue() > 0;
	}

	@Override
	public void insertCtnUserMsg(CtnUserMsg msg) {
		getHibernateTemplate().persist(msg);
		
	}
	@Override
	public CtnMsgTempl getMsgTemplByID(Long ID) {
		return getHibernateTemplate().get(CtnMsgTempl.class, ID);
		
	}
	
}
